<template>
  <div class="loginForm">
    <el-form
      ref="formRef"
      :model="formVals"
      label-width="80px"
      class="demo-dynamic"
      :rules="rules"
    >
      <el-form-item prop="username" label="用户名">
        <el-input v-model.trim="formVals.username" :prefix-icon="UserFilled" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model.trim="formVals.password"
          :prefix-icon="Lock"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          round
          class="loginBtn"
          auto-insert-space
          @click="submitForm(formRef)"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { UserFilled, Lock } from "@element-plus/icons-vue";
import type { FormInstance } from "element-plus";
import useStore from "@/store";
export type TloginForm = { username: string; password: string };
const emit = defineEmits(["setVisible"]);
const { user } = useStore();
const formRef = ref<FormInstance>();
let formVals = ref<TloginForm>({
  username: "",
  password: "",
});
const rules = ref({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});
const submitForm = (formRef: FormInstance | undefined) => {
  formRef?.validate(async (valid) => {
    if (!valid) return;
    let res = await user.userLogin(formVals.value);
    console.log(res);
    emit("setVisible");
  });
};
</script>

<style scoped lang="less">
.loginForm {
  padding-right: 20px;
}
.loginBtn {
  width: 100%;
}
</style>
